<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="供应商名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入供应商名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:faDeviceFirm:addOrUpdate']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="固件名称" align="center" prop="name" />
      <el-table-column label="固件类型" align="center" prop="firmType" >
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.firmware_type" :value="row.firmType"/>
        </template>
      </el-table-column>
      <el-table-column label="支持硬件版本" align="center" prop="supportHw" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['system:faDeviceFirm:addOrUpdate']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-hasPermi="['system:faDeviceFirm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="固件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入固件名称" />
        </el-form-item>
        <el-form-item label="固件类型" prop="firmType">
          <el-radio-group v-model="form.firmType">
            <el-radio v-for="(item,index) in dict.type.firmware_type" :key="index" :label="Number(item.value)">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="固件路径" prop="file">
          <FileUpload
              :isShowTip="false"
              :limit="1"
              v-model="form.file"
              accept="*"
              :fileType="''"></FileUpload>
        </el-form-item>
        <el-form-item label="支持硬件版本" prop="supportHw">
          <el-select v-model="form.supportHw" multiple placeholder="请选择支持硬件版本" >
            <el-option
              v-for="item in yingjianList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addOrUpdate, delFaDevice, uploadFaDevice,} from "@/api/parking/firmware";
import { getAllHwVersion,} from "@/api/parking/devices";
export default {
  name: "Firmware",
  dicts: ['firmware_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "固件名称不能为空", trigger: "blur" }],
        firmType: [{ required: true, message: "固件类型不能为空", trigger: "change" }],
      },
      yingjianList:[],//硬件列表
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getAllHwVersion(){//查询所有设备固件列表
      let res = await getAllHwVersion()
      let data = res.data || []
      this.yingjianList = data.map(item=>{
        return {...item,name:item,id:item}
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then(response => {
          this.list = response.rows || [];
          this.total = response.total || 0;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {firmType:0,};
      this.resetForm("form");
      this.getAllHwVersion()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加固件管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      let supportHw = row.supportHw || ''
      this.form = {...row,supportHw:supportHw ? supportHw.split(',') : []};
      this.open = true;
      this.title = "修改固件管理";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          if(form.supportHw) form.supportHw = form.supportHw.toString()
          addOrUpdate(form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id
      this.$modal.confirm('是否确认删除该数据项？').then(function() {
        return delFaDevice(dictIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
